/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014-7-4         admin
 */

package com.tianci.appstore.ui.search;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.tianci.appstore.utils.SkyConstants;

/**
 * @author admin
 * @ClassName SearchKeyItemView
 * @date 2014-7-4
 */
public class SearchKeyItemView extends FrameLayout {
    public static int IS_T9 = 1;
    public static int NOT_T9 = 2;
    private Context mContext;
    public TextView keyText;
    private int myItemType;

    private int x_position = 0, y_position = 0;
    protected int width = 0, height = 0;

    private OnItemClickListener myListener;

    public interface OnItemClickListener {
        public void itemClickEvent(String keyValue);

        public void t9_itemClickEvent(SearchKeyItemView view);

        public void onItemFocusEvent(SearchKeyItemView view);
    }

    public SearchKeyItemView(Context context, int bgid, int itemType, int w, int h) {
        super(context);
        width = w + SkyConstants.Div(80);
        height = h + SkyConstants.Div(80);
        myItemType = itemType;
        mContext = context;
        initView();
        this.setOnClickListener(itemClickListener);
        this.setOnFocusChangeListener(itemFocusListener);
        FrameLayout.LayoutParams this_p = new FrameLayout.LayoutParams(width, height);
        this_p.gravity = Gravity.CENTER;
        this.setLayoutParams(this_p);
        this.setFocusable(true);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        myListener = listener;
    }

    private void initView() {
        keyText = new TextView(mContext);
        FrameLayout.LayoutParams key_p = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        key_p.gravity = Gravity.CENTER;
        this.addView(keyText, key_p);
        setTextAttribute(SkyConstants.Dpi(38), 0xffb2b2b2);
    }

    public void setTextAttribute(int textSize, int textColor) {
        keyText.setTextSize(textSize);
        keyText.setTextColor(textColor);
    }

    public void setTextValue(int strid) {
        keyText.setText(strid);
    }

    OnClickListener itemClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (myItemType == IS_T9) {
                if (myListener != null)
                    myListener.t9_itemClickEvent(SearchKeyItemView.this);
            } else if (myItemType == NOT_T9) {
                if (myListener != null)
                    myListener.itemClickEvent(keyText.getText().toString());
            }
        }
    };

    OnFocusChangeListener itemFocusListener = new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {

            if (hasFocus) {
                keyText.setTextColor(0xffffffff);
                keyText.getPaint().setFakeBoldText(true);
                if (myListener != null)
                    myListener.onItemFocusEvent((SearchKeyItemView) v);
            } else {
                keyText.setTextColor(0xffb2b2b2);
                keyText.getPaint().setFakeBoldText(false);
            }
        }
    };

    public int getXPostion() {
        return x_position;
    }

    public void setXPosition(int x) {
        x_position = x;
    }

    public int getYPostion() {
        return y_position;
    }

    public void setYPosition(int y) {
        y_position = y;
    }

    public int getWidthSize() {
        return width;
    }

    public void setWidthSize(int w) {
        width = w;
    }

    public int getHeightSize() {
        return height;
    }

    public void setHeightSize(int h) {
        height = h;
    }
}
